﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Globalization;

using QuickDev.Common.Audit;
using QuickDev.Common.Globalization;

using QuickDev.Localization;
using QuickDev.Model;
using QuickDev.Caching;

#endregion

namespace QuickDev.Web.App.Administration.Controls.SiteSettingConfiguration
{
    public partial class globalization : SiteSettingsControl
    {
        #region Member Variables

        #endregion

        #region Page Events

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected override void Page_Load( object sender, EventArgs e )
        {
            try
            {
                base.Page_Load( sender, e );
                if ( string.IsNullOrEmpty( view ) || view.Equals( "gl" ) ) //since it is default view
                {
                    SetGlobalizationSettingsProperties();
                    if ( !Page.IsPostBack )
                    {
                        SetLanguageSelections();
                        SetCurrencySelections();
                        ddlLanguage.SelectedValue = SiteSettings.Language;
                        ddlCurrency.SelectedValue = SiteSettings.CurrencyCode;
                    }
                }
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( globalization ).Name + ".Page_Load", ex );
                base.MasterPage.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click( object sender, EventArgs e )
        {
            try
            {
                SiteSettings.Language = ddlLanguage.SelectedValue;
                SiteSettings.CurrencyCode = ddlCurrency.SelectedValue;
                ServiceConfiguration model = base.SaveConfiguration( SiteSettings );
                if ( model.ConfigurationId > 0 )
                {
                    MasterPage.MessageCenter.DisplaySuccessMessage( LocalizationUtility.GetText( "lblSiteSettingsSaved" ) );
                }
                else
                {
                    MasterPage.MessageCenter.DisplayFailureMessage( LocalizationUtility.GetText( "lblSiteSettingsNotSaved" ) );
                }
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( globalization ).Name + ".btnSave_Click", ex );
                base.MasterPage.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        #endregion

        #region Methods

        #region Private

        /// <summary>
        /// Sets the language selections.
        /// </summary>
        private void SetLanguageSelections()
        {
            CultureInfo[] cultures = CultureInfo.GetCultures( CultureTypes.SpecificCultures );
            Array.Sort( cultures, new CultureComparer() );
            ddlLanguage.DataSource = cultures;
            ddlLanguage.DataValueField = "Name";
            ddlLanguage.DataTextField = "DisplayName";
            ddlLanguage.DataBind();
        }

        /// <summary>
        /// Sets the currency selections.
        /// </summary>
        private void SetCurrencySelections()
        {
            List<CurrencyCode> currencyCollection = CurrencyCodeCache.GetList();
            ddlCurrency.DataSource = currencyCollection;
            ddlCurrency.DataValueField = "Code";
            ddlCurrency.DataTextField = "Description";
            ddlCurrency.DataBind();
        }

        /// <summary>
        /// Sets the globalization settings properties.
        /// </summary>
        private void SetGlobalizationSettingsProperties()
        {
            this.Page.Title = string.Format( WebUtility.MainTitleTemplate, base.MasterPage.SiteSettings.SiteName, LocalizationUtility.GetText( "titleSiteSettingsGlobalization" ) );
        }

        #endregion

        #endregion

    }
}